package com.whg.es.controller;

import com.whg.es.model.NBAPlayer;
import com.whg.es.service.NBAPlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Author whg
 * @create 2023/7/5 18:26
 * @description：
 */
@RestController
@RequestMapping("/nba")
public class NBAPlayerController {

    @Autowired
    private NBAPlayerService nbaPlayerService;

    /**
     * 导入所有的运动员
     * @return
     */
    @RequestMapping("/importAll")
    public String importAll(){
        try {
            nbaPlayerService.importAllPlayer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

    /**
     * 匹配查询运动员
     * @param field
     * @param text
     * @return
     */
    @RequestMapping("/searchMatch")
    public List<NBAPlayer> searchMatch(@RequestParam(name = "field") String field,
                                       @RequestParam(name = "text") String text){
        List<NBAPlayer> list = null;
        try{
            list = nbaPlayerService.searchMatch(field,text);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 精准查询运动员
     * @param field
     * @param text
     * @return
     */
    @RequestMapping("/searchTerm")
    public List<NBAPlayer> searchTerm(@RequestParam(name = "field") String field,
                                       @RequestParam(name = "text") String text){
        List<NBAPlayer> list = null;
        try{
            list = nbaPlayerService.searchTerm(field,text);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 前缀匹配查询运动员
     * @param field
     * @param text
     * @return
     */
    @RequestMapping("/searchPrefix")
    public List<NBAPlayer> searchPrefix(@RequestParam(name = "field") String field,
                                      @RequestParam(name = "text") String text){
        List<NBAPlayer> list = null;
        try{
            list = nbaPlayerService.searchPrefix(field,text);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
